package com.isoft.illustrationsdraw.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.dto.NewIllustrationDTO;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.isoft.illustrationsdraw.service.FileService;
import com.isoft.illustrationsdraw.service.JWTService;
import com.isoft.illustrationsdraw.service.MediaService;
import com.isoft.illustrationsdraw.service.ResourceService;
import com.isoft.illustrationsdraw.service.UserService;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("api/media")
public class MediaController {

    private MediaService mediaService;
    private ResourceService resourceService;
    private JWTService jwtService;
    private FileService fileService;
    private UserService userService;

    public MediaController(MediaService mediaService,ResourceService resourceService, JWTService jwtService, FileService fileService, UserService userService) {
        this.mediaService = mediaService;
        this.resourceService = resourceService;
        this.jwtService = jwtService;
        this.fileService = fileService;
        this.userService = userService;

    }

    @PostMapping("/uploadProfileImage")
    public ResponseEntity<String> uploadProfileImage(@RequestParam String token, @RequestParam MultipartFile file) {
        try {
            if (!jwtService.validateToken(token)) {
                return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
            }
            mediaService.uploadProfileImage(token, file);

            Long userId = jwtService.getUserIdFromToken(token);
            var profileImg = userService.getById(userId).getProfileImg();
           
            return ResponseEntity.ok(profileImg == null ? null : fileService.convertToOnlinePath(profileImg));
        } catch (IllegalArgumentException | IOException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }


    @PostMapping("/uploadIllustration")
    public ResponseEntity<String> handleUploadIllustrationRequest(
            @RequestParam String token,
            @RequestParam String newIllustrationDTO,  // 接收 JSON 字符串
            @RequestParam List<MultipartFile> files) {
        try {
            // 将 JSON 字符串解析为 NewIllustrationDTO 对象
            ObjectMapper objectMapper = new ObjectMapper();
            NewIllustrationDTO dto = objectMapper.readValue(newIllustrationDTO, NewIllustrationDTO.class);

            // 调用服务处理逻辑
            mediaService.handleUploadIllustrationRequest(token, dto, files);

            return ResponseEntity.ok("Illustration uploaded successfully.");
        } catch (IllegalArgumentException | IOException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }


    @GetMapping("/illustration/{id}/{index}")
    public ResponseEntity<Resource> getIllustrationResourceById(@PathVariable Long id, @PathVariable Integer index) {
        try {
            Resource resource = resourceService.getSingleIllustrationResourceById(id, index);
            return ResponseEntity.ok(resource);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(null);
        } catch (IOException e) {
            return ResponseEntity.notFound().build();
        }
    }
}
